package com.teamsun.mqtt.entity.vo;

import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * 延迟发送对象
 * @author acer
 *延迟队列也应该有bytebuf 这样效率高
 */
public class DelaySnedMsg implements  Delayed{

	SendableMsg sendableMsg;
	
	/**
	 * 将要重发的时间  单位毫秒
	 */
	long  willSendTime;
	
  
	volatile boolean  hasSend;
	

	
	public DelaySnedMsg(long willSendTime){
		
		this.willSendTime=willSendTime;
		
		
	}
	

	
	public DelaySnedMsg(SendableMsg sendableMsg) {
		super();
		this.sendableMsg = sendableMsg;	
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.SECOND, 5 * (sendableMsg.getDupTimes() + 1));
		willSendTime=calendar.getTimeInMillis();
	}


	@Override
	public int compareTo(Delayed o) {
		
		if(o instanceof  DelaySnedMsg){
			DelaySnedMsg delaySnedMsg=(DelaySnedMsg)o;
			return (int)(this.willSendTime-delaySnedMsg.willSendTime);
		}
		return 1;
	}

	@Override
	public long getDelay(TimeUnit unit) {
	
		return 	unit.convert(
				willSendTime-System.currentTimeMillis(),
				TimeUnit.MILLISECONDS);
	}


	public SendableMsg getSendableMsg() {
		return sendableMsg;
	}


	public void setSendableMsg(SendableMsg sendableMsg) {
		this.sendableMsg = sendableMsg;
	}



	public boolean isHasSend() {
		return hasSend;
	}

	public void setHasSend(boolean hasSend) {
		this.hasSend = hasSend;
	}


	
  
}
